//
//  TabBarView.swift
//  SwiftCode
//
//  Created by yuhus on 2023/12/11.
//

import Foundation
import UIKit
import RxSwift

final class TabBarView: UIView {
  let didSelectItem = PublishSubject<Int>()
  
  var selectedIndex: Int = 0 {
    didSet {
      updateSelected(oldValue, selected: false)
      updateSelected(selectedIndex, selected: true)
    }
  }
  
  var shadowImage: UIImage? {
    didSet { shadowView.image = shadowImage }
  }
  
  var backgroundImage: UIImage? {
    didSet { backgroundView.image = backgroundImage }
  }
  
  override var tintColor: UIColor! {
    didSet {}
  }
  
  var unselectedItemTintColor: UIColor? {
    didSet {}
  }
  
  var items: [UITabBarItem]? {
    didSet {
      updateItems()
    }
  }
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    
    initView()
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  private func initView() {
    [shadowView, backgroundView, container].forEach {
      addSubview($0)
    }
    
    shadowView.snp.makeConstraints { make in
      make.height.equalTo(0.5)
      make.bottom.equalTo(self.snp.top)
      make.leading.trailing.equalToSuperview()
    }
    backgroundView.snp.makeConstraints { make in
      make.edges.equalToSuperview()
    }
    container.snp.makeConstraints { make in
      make.edges.equalToSuperview()
    }
  }
  
  private func updateItems() {
    guard let items else { return }
    
    for (index, item) in items.enumerated() {
      let button = TabBarButton()
      button.item = item
      button.tag = index
      button.addTarget(self, action: #selector(buttonOnTapped(_:)), for: .touchUpInside)
      container.addArrangedSubview(button)
    }
  }
  
  private func updateSelected(_ index: Int, selected: Bool) {
    guard let button = container.subviews[index] as? TabBarButton else { return }
    button.isSelected = selected
  }
  
  @objc private func buttonOnTapped(_ sender: TabBarButton) {
    let index = sender.tag
    Timber.d(#function, index)
    didSelectItem.onNext(index)
  }
  
  private lazy var shadowView: UIImageView = {
    let view = UIImageView()
    view.backgroundColor = .neutral7
    return view
  }()
  
  private lazy var backgroundView: UIImageView = {
    let view = UIImageView()
    return view
  }()
  
  private lazy var container: UIStackView = {
    let stack = UIStackView()
    stack.axis = .horizontal
    stack.distribution = .fillEqually
    return stack
  }()
}

final private class TabBarButton: UIControl {
  var item: UITabBarItem? {
    didSet { updateView(isSelected) }
  }
  
  override var isSelected: Bool {
    didSet {
      updateView(isSelected)
    }
  }
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    initView()
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  private func initView() {
    [imageView, titleLabel].forEach { addSubview($0) }
    
    imageView.snp.makeConstraints { make in
      make.size.equalTo(30)
      make.top.equalToSuperview().inset(6.0)
      make.centerX.equalToSuperview()
    }
    
    titleLabel.snp.makeConstraints { make in
      make.top.equalTo(imageView.snp.bottom).offset(2)
      make.centerX.equalToSuperview()
    }
  }
  
  private func updateView(_ selected: Bool) {
    imageView.image = selected ? item?.selectedImage : item?.image
    titleLabel.text = item?.title
  }
  
  private lazy var imageView: UIImageView = {
    let view = UIImageView()
    return view
  }()
  
  private lazy var titleLabel: UILabel = {
    let label = UILabel()
    label.font = .poppinsFont(ofSize: 10)
    label.textColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
    return label
  }()
}

extension Reactive where Base: TabBarView {
  var items: Binder<[UITabBarItem]?> {
    return Binder(base) { $0.items = $1 }
  }
}
